//
//  KWNMyCommentViewController.m
//  KWNClient
//
//  Created by 张明磊 on 14/11/1.
//  Copyright (c) 2014年 张明磊. All rights reserved.
//

#import "KWLMyCommentViewController.h"
#import "KWNNetwork.h"
#import "Model_lawyerInfo.h"
#import "Model_myComment.h"
#import "UIButton+WebCache.h"
#import "TQStarRatingView.h"
#import "KWLLayerViewController.h"
#import "KWLLoginViewController.h"
//#import "UMSocial.h"
//#import "UMSocialControllerService.h"
//#import "UMSocialDataService.h"

@interface KWLMyCommentViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSString *string_id;
@property (strong, nonatomic) NSMutableArray *array_data;
@property (weak, nonatomic) UIView *imageView_title;

@property (strong, nonatomic) NSArray *array_location;

@end

@implementation KWLMyCommentViewController

- (id)initWIthQuestionID:(NSString *)string_id{
    self = [super init];
    if(self){
        _string_id = string_id;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self UI_myTitle];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *array_tmp = @[
                               @"北京",@"上海",@"天津",@"重庆",@"黑龙江省",@"吉林省",@"辽宁省",@"河北省",@"山西省",@"内蒙古自治区",@"山东省",@"安徽省",@"江苏省",@"浙江省",@"江西省",@"福建省",@"河南省",@"湖北省",@"湖南省",@"广东省",@"广西壮族自治区",@"海南省",@"四川省",@"贵州省",@"云南省",@"西藏自治区",@"陕西省",@"甘肃省",@"青海省",@"宁夏回族自治区",@"新疆维吾尔自治区",@"香港特别行政区",@"澳门特别行政区",@"台湾省"];
        dispatch_async(dispatch_get_main_queue(), ^{
            _array_location = array_tmp;
        });
    });
    [self UI_myComment];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)UI_myTitle{
    self.view.backgroundColor = RGB(239, 239, 244);
    
    UIView *imageView_title = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44 + IOS7_SIZE)];
    imageView_title.backgroundColor = RGB(91, 165, 229);
    [self.view addSubview:imageView_title];
    _imageView_title = imageView_title;
    
    UIButton *button_menu = [UIButton buttonWithType:UIButtonTypeCustom];
    button_menu.frame = CGRectMake(0, IOS7_SIZE, 60, 45);
    [button_menu setImage:[UIImage imageFileName:@"button_back.png"] forState:UIControlStateNormal];
    [button_menu addTarget:self action:@selector(didClickButton_myComment:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_menu];
    
    UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(imageView_title.frame.origin.x,IOS7_SIZE + 5,imageView_title.frame.size.width,35)];
    label_title.backgroundColor = [UIColor clearColor];
    label_title.textColor = RGB(63, 119, 167);
    label_title.textAlignment = NSTextAlignmentCenter;
    label_title.font = [UIFont fontWithName:@"Helvetica-Bold" size:16.0f];
    [self.view addSubview:label_title];
    label_title.text = @"我的评价";
}

- (void)UI_myComment{
    //[UIUtils view_showProgressHUD:@"加载中..." inView:self.view withType:0];
    [[KWNNetwork sharedInstance] question_getComment:_string_id FinishBlock:^(NSInteger stateCode, NSMutableArray *result) {
        //[UIUtils view_hideProgressHUDinView:self.view];
        switch (stateCode) {
            case 10000:{
                _array_data = result;
                [self UI_drawComment];
            }break;
            case 10003:{
                UIAlertView *alter_login;
                if(IOS8_VERSION){
                    alter_login = [[UIAlertView alloc] initWithTitle:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                else{
                    alter_login = [[UIAlertView alloc] initWithTitle:nil message:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                alter_login.tag = 10003;
                [alter_login show];

//                NSString *string_msg  = result[0];
//                [UIUtils view_showProgressHUD:string_msg inView:self.view withTime:1.5f];
//                
//                double delayInSeconds = 1.5;
//                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//                    //code to be executed on the main queue after delay
//                    KWLLoginViewController *login = [[KWLLoginViewController alloc] initWithTag:6];
//                    [self pushViewController:login];
//                });
                //[self ]
            }break;

            default:{
                NSString *string_msg  = result[0];
// CS 快法务 KLW 2014-12-24
//                [UIUtils view_showProgressHUD:string_msg inView:self.view withTime:1.5f];
//                
                [SVProgressHUD showErrorWithStatus:string_msg duration:1.5];
// CE 快法务 KLW 2014-12-24
            }break;
        }
    }];
}

- (void)didClickButton_myComment:(UIButton *)button{
    [self popViewController];
}

- (void)UI_drawComment{
    UITableView *tableView_tabelView_body = [[UITableView alloc] initWithFrame:CGRectMake(0, _imageView_title.frame.origin.y + _imageView_title.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - _imageView_title.frame.size.height)];
    tableView_tabelView_body.backgroundColor = [UIColor clearColor];
    tableView_tabelView_body.delegate = self;
    tableView_tabelView_body.dataSource = self;
    [self.view addSubview:tableView_tabelView_body];
    tableView_tabelView_body.separatorColor = [UIColor clearColor];
    
    /*UIImageView *imageView_zhuijia = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 65, 320, 65)];
    imageView_zhuijia.image = [UIImage imageFileName:@"image_pinglunBack.png"];
    [self.view addSubview:imageView_zhuijia];
    
    UIButton *button_share = [UIButton buttonWithType:UIButtonTypeCustom];
    button_share.frame = CGRectMake(60, imageView_zhuijia.frame.origin.y + 15, 201, 36);
    button_share.backgroundColor = [UIColor clearColor];
    [button_share setImage:[UIImage imageFileName:@"button_shareOther.png"] forState:UIControlStateNormal];
    [button_share addTarget:self action:@selector(didClickButton_share:)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_share];*/
}

#pragma mark - alertView -
/**
 *  账号被替换下线
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (alertView.tag) {
            //账号替换
        case 10003:{
            KWLLoginViewController *login = [[KWLLoginViewController alloc] initWithTag:6];
            [self pushViewController:login];
        }break;
            
        default:
            break;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"myCommentCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        Model_myComment *myComment = [_array_data objectAtIndex:0];
        
        UIImageView *imageView_user = [[UIImageView alloc] initWithFrame:CGRectMake(25, 25, 12, 14)];
        imageView_user.image = [UIImage imageFileName:@"image_free_Person.png"];
        [cell addSubview:imageView_user];
        
        UILabel *label_name = [[UILabel alloc] initWithFrame:CGRectMake(imageView_user.frame.origin.x + imageView_user.frame.size.width + 5, imageView_user.frame.origin.y - 4, 100, 20)];
        label_name.backgroundColor = [UIColor clearColor];
        NSInteger kind_areaName = myComment.string_area.integerValue;
        NSString *string_name = _array_location[kind_areaName];
        label_name.text = [NSString stringWithFormat:@"%@网友",string_name];
        label_name.textColor = RGB(209, 209, 209);
        label_name.textAlignment = NSTextAlignmentLeft;
        label_name.font = [UIFont fontWithName:@"Helvetica-Bold" size:13.0f];
        [cell addSubview:label_name];
        
        UIView *view_one = [[UIView alloc] initWithFrame:CGRectMake(imageView_user.frame.origin.x, imageView_user.frame.origin.y + imageView_user.frame.size.height + 10, 60, 20)];
        view_one.backgroundColor = RGB(253, 204, 67);
        [cell addSubview:view_one];
        //设置矩形四个圆角半径
        view_one.layer.cornerRadius = 10.0;
        
        NSInteger kind = myComment.string_goodatfield.integerValue;
        UILabel *label_one = [[UILabel alloc] initWithFrame:view_one.frame];
        label_one.backgroundColor = [UIColor clearColor];
        if(kind < 10){
            label_one.text = @"个人问题";
        }
        else{
            label_one.text = @"公司问题";
        }
        label_one.textColor = RGB(200, 137, 46);
        label_one.textAlignment = NSTextAlignmentCenter;
        label_one.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
        [cell addSubview:label_one];
        
        UIView *view_three = [[UIView alloc] initWithFrame:CGRectMake(view_one.frame.origin.x + view_one.frame.size.width + 5, view_one.frame.origin.y, 60, 20)];
        view_three.backgroundColor = RGB(253, 204, 67);
        [cell addSubview:view_three];
        //设置矩形四个圆角半径
        view_three.layer.cornerRadius = 10.0;
        
        NSInteger kind_content = myComment.string_goodatfield.integerValue;
        UILabel *label_three = [[UILabel alloc] initWithFrame:view_three.frame];
        label_three.backgroundColor = [UIColor clearColor];
        switch (kind_content) {
            case 0:{
                label_three.text = @"婚姻家庭";
            }break;
            case 1:{
                label_three.text = @"房屋买卖";
            }break;
            case 2:{
                label_three.text = @"债务纠纷";
            }break;
            case 3:{
                label_three.text = @"劳动争议";
            }break;
            case 4:{
                label_three.text = @"交通肇事";
            }break;
            case 5:{
                label_three.text = @"刑事案件";
            }break;
            case 6:{
                label_three.text = @"其他类型";
            }break;
            case 11:{
                label_three.text = @"公司注册";
            }break;
            case 12:{
                label_three.text = @"投资融资";
            }break;
            case 13:{
                label_three.text = @"股权期权";
            }break;
            case 14:{
                label_three.text = @"合同事项";
            }break;
            case 15:{
                label_three.text = @"知识产权";
            }break;
            case 16:{
                label_three.text = @"财税事务";
            }break;
            case 17:{
                label_three.text = @"劳动人事";
            }break;
            case 18:{
                label_three.text = @"债务纠纷";
            }break;
            case 19:{
                label_three.text = @"其他问题";
            }break;
            default:
                break;
        }
        label_three.textColor = RGB(200, 137, 46);
        label_three.textAlignment = NSTextAlignmentCenter;
        label_three.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
        [cell addSubview:label_three];
        UIImageView *imageView_pinglun = [[UIImageView alloc] initWithFrame:CGRectMake(24, view_three.frame.origin.y + 40, 272, 8)];
        imageView_pinglun.image = [UIImage imageFileName:@"chenggongpingjia.png"];
        [cell addSubview:imageView_pinglun];
        
        UILabel *label_sudu = [[UILabel alloc] initWithFrame:CGRectMake(imageView_pinglun.frame.origin.x, imageView_pinglun.frame.origin.y + 20, 80, 35)];
        label_sudu.backgroundColor = [UIColor clearColor];
        label_sudu.text = @"响应速度：";
        label_sudu.textColor = RGB(120, 120, 120);
        label_sudu.textAlignment = NSTextAlignmentLeft;
        label_sudu.font = [UIFont fontWithName:@"Helvetica" size:15.0f];
        [cell addSubview:label_sudu];
        
        TQStarRatingView *starRatingView_sudu = [[TQStarRatingView alloc] initWithFrame:CGRectMake(label_sudu.frame.origin.x + label_sudu.frame.size.width, label_sudu.frame.origin.y + 5,185, 25)
                                                                           numberOfStar:kNUMBER_OF_STAR];
        //starRatingView_sudu.delegate = self;
        [starRatingView_sudu setScore:myComment.string_xy_fraction.floatValue/5.0 withAnimation:NO];
        starRatingView_sudu.backgroundColor = RGB(239, 239, 244);
        [cell addSubview:starRatingView_sudu];
        
        UILabel *label_taidu = [[UILabel alloc] initWithFrame:CGRectMake(label_sudu.frame.origin.x, label_sudu.frame.origin.y + label_sudu.frame.size.height + 10, label_sudu.frame.size.width, label_sudu.frame.size.height)];
        label_taidu.backgroundColor = [UIColor clearColor];
        label_taidu.text = @"服务态度：";
        label_taidu.textColor = RGB(120, 120, 120);
        label_taidu.textAlignment = NSTextAlignmentLeft;
        label_taidu.font = [UIFont fontWithName:@"Helvetica" size:15.0f];
        [cell addSubview:label_taidu];
        
        TQStarRatingView *starRatingView_taidu = [[TQStarRatingView alloc] initWithFrame:CGRectMake(starRatingView_sudu.frame.origin.x, label_taidu.frame.origin.y + 5,starRatingView_sudu.frame.size.width, starRatingView_sudu.frame.size.height)
                                                                            numberOfStar:kNUMBER_OF_STAR];
        starRatingView_taidu.backgroundColor = RGB(239, 239, 244);
        //starRatingView_taidu.delegate = self;
        [starRatingView_taidu setScore:myComment.string_fuwu_fraction.floatValue/5.0 withAnimation:NO];
        [cell addSubview:starRatingView_taidu];
        
        UILabel *label_zhuanye = [[UILabel alloc] initWithFrame:CGRectMake(label_taidu.frame.origin.x, label_taidu.frame.origin.y + label_taidu.frame.size.height + 10, label_taidu.frame.size.width, label_taidu.frame.size.height)];
        label_zhuanye.backgroundColor = [UIColor clearColor];
        label_zhuanye.text = @"专业程度：";
        label_zhuanye.textColor = RGB(120, 120, 120);
        label_zhuanye.textAlignment = NSTextAlignmentLeft;
        label_zhuanye.font = [UIFont fontWithName:@"Helvetica" size:15.0f];
        [cell addSubview:label_zhuanye];
        
        TQStarRatingView *starRatingView_zhuanye = [[TQStarRatingView alloc] initWithFrame:CGRectMake(starRatingView_taidu.frame.origin.x, label_zhuanye.frame.origin.y + 5,starRatingView_taidu.frame.size.width, starRatingView_taidu.frame.size.height)
                                                                              numberOfStar:kNUMBER_OF_STAR];
        starRatingView_zhuanye.backgroundColor = RGB(239, 239, 244);
        //starRatingView_zhuanye.delegate = self;
        [cell addSubview:starRatingView_zhuanye];
        [starRatingView_zhuanye setScore:myComment.string_ziliang_fraction.floatValue/5.0 withAnimation:NO];
        
        [starRatingView_sudu setUserInteractionEnabled:NO];
        [starRatingView_taidu setUserInteractionEnabled:NO];
        [starRatingView_zhuanye setUserInteractionEnabled:NO];
        
        UIImageView *imageView_wenzi = [[UIImageView alloc] initWithFrame:CGRectMake(24, label_zhuanye.frame.origin.y + label_zhuanye.frame.size.height + 20, 272, 8)];
        imageView_wenzi.image = [UIImage imageFileName:@"wenzipinglun.png"];
        [cell addSubview:imageView_wenzi];
        
        UITextView *textView_content = [[UITextView alloc] initWithFrame:CGRectMake(20, imageView_wenzi.frame.origin.y + 20, 290, 200)];
        textView_content.backgroundColor = [UIColor clearColor
                                            ];
        textView_content.text = myComment.string_content;
        textView_content.textColor = RGB(164, 163, 167);
        textView_content.font = [UIFont fontWithName:@"Helvetica-Bold" size:13.0f];
        textView_content.textAlignment = NSTextAlignmentNatural;
        textView_content.autocorrectionType = UITextAutocorrectionTypeNo;
        textView_content.autocapitalizationType = UITextAutocapitalizationTypeNone;
        textView_content.scrollEnabled = NO;
        [textView_content setEditable:NO];
        CGSize size_text = [self contentSizeOfTextView:textView_content];
        textView_content.frame = CGRectMake(textView_content.frame.origin.x, textView_content.frame.origin.y, size_text.width, size_text.height);
        [cell addSubview:textView_content];
    }
    return cell;
}

- (CGSize)contentSizeOfTextView:(UITextView *)textView{
    CGSize textViewSize = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, FLT_MAX)];
    return textViewSize;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Model_myComment *myComment = [_array_data objectAtIndex:0];
    UITextView *textView_content = [[UITextView alloc] initWithFrame:CGRectMake(20, 70, 290, 200)];
    textView_content.backgroundColor = [UIColor clearColor
                                        ];
    textView_content.text = myComment.string_content;
    textView_content.textColor = RGB(93, 93, 93);
    textView_content.font = [UIFont fontWithName:@"Helvetica_bold" size:13.0f];
    textView_content.textAlignment = NSTextAlignmentNatural;
    textView_content.autocorrectionType = UITextAutocorrectionTypeNo;
    textView_content.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textView_content.scrollEnabled = NO;
    [textView_content setEditable:NO];
    CGSize size_text = [self contentSizeOfTextView:textView_content];
    
    return size_text.height + 450;
}

#pragma mark - 分享模块 -
/**
 *  分享模块
 */
- (void)didClickButton_share:(UIButton *)button{
    
//    [UMSocialSnsService presentSnsIconSheetView:self
//                                         appKey:nil
//                                      shareText:@"#如果那时 ……人生或许不同#。好东西不独享:http://t.cn/arUsQs"
//                                     shareImage:[UIImage imageFileName:@"kfw_portrait.png"]
//                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatFavorite,UMShareToWechatSession,nil]
//                                       delegate:self];
}

- (void)didClickButton_laywer:(UIButton *)button{
    Model_myComment *myComment = [_array_data objectAtIndex:0];
    KWLLayerViewController *laywer = [[KWLLayerViewController alloc] initWithLaywerWithID:myComment.string_id];
    [self pushViewController:laywer];
}

//-(void)didSelectSocialPlatform:(NSString *)platformName withSocialData:(UMSocialData *)socialData{
//    //UINavigationController *navi = [tmp currentNavigationController];
//    //controller.navigationController.navigationBarHidden = YES;
//}
//
////实现回调方法（可选）：
//-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
//{
//    //根据`responseCode`得到发送结果,如果分享成功
//    if(response.responseCode == UMSResponseCodeSuccess)
//    {
//        //得到分享到的微博平台名
//        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
//    }
//}
//
//-(void)didCloseUIViewController:(UMSViewControllerType)fromViewControllerType{
//    
//    /*UMSocialControllerService *controller_service = [UMSocialControllerService defaultControllerService];
//    UIViewController *controller = controller_service.currentViewController;
//    UINavigationController *nav_service = controller_service.currentNavigationController;
//    [nav_service.navigationBar setHidden:YES];
//    
//    
//    [controller.navigationController.navigationBar setBarTintColor:RGB(91, 165, 229)];
//    [nav_service.navigationBar setTranslucent:NO];
//    [nav_service setTitle:@"来问律师"];*/
//    
//    
//    //UIView *p = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];
//    //p.backgroundColor = [UIColor blackColor];
//    //[controller.view addSubview:p];
//}
//
//- (UIViewController *)getSocialViewController:(UMSViewControllerType)viewControllerType withSnsType:(NSString *)snsName{
//    UIViewController *tmp = [[UIViewController alloc] init];
//    return tmp;
//}

@end
